package com.ukao.cashregister.ui.arriveShop;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.ReceiveProClothingListAdapter;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.bean.ReceiveClothingBean;
import com.ukao.cashregister.bean.ReceiveProClothingBean;
import com.ukao.cashregister.bean.ReceiveProListBean;
import com.ukao.cashregister.bean.ReceiveStoreBean;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.pesenter.ArriveShopPesenter;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.MyDateUtils;
import com.ukao.cashregister.utils.SystemUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.view.ArriveShopHomeView;
import com.ukao.cashregister.view.ArriveShopView;
import com.ukao.cashregister.widget.ClearEditText;
import com.ukao.cashregister.widget.DividerDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 衣物到店fragmant
 */
public class ClothingArriveShopFragment extends MvpFragment<ArriveShopPesenter> implements ArriveShopView {


    @BindView(R.id.arrive_shop_to_recycler)
    RecyclerView mToRecycler;
    Unbinder unbinder;
    ReceiveProClothingListAdapter mToAdapter;

    @BindView(R.id.fragment_arrive_shop_search1_btn)
    Button fragmentArriveShopSearch1Btn;
    @BindView(R.id.search_clothcode)
    ClearEditText searchClothcode;
    @BindView(R.id.code)
    TextView code;
    @BindView(R.id.storeName)
    TextView storeName;
    @BindView(R.id.factoryName)
    TextView factoryName;
    @BindView(R.id.factoryWorkTime)
    TextView factoryWorkTime;

    @BindView(R.id.arrive_shop_clothing_from)
    TextView arriveShopClothingFrom;
    @BindView(R.id.arrive_shop_clothing_to)
    TextView arriveShopClothingTo;
    @BindView(R.id.arrive_shop_operator)
    TextView arriveShopOperator;
    @BindView(R.id.code_describe)
    TextView codeDescribe;
    @BindView(R.id.arrive_to_factory)
    ImageView arriveToFactory;
    @BindView(R.id.leave_factory_text)
    TextView leaveFactoryText;
    @BindView(R.id.remark)
    TextView remark;
    @BindView(R.id.remark_btn)
    Button remarkBtn;
    @BindView(R.id.arrive_show_b1)
    Button arriveShowB1;
    @BindView(R.id.arrive_show_b2)
    Button arriveShowB2;
    private ArrayList<ReceiveClothingBean> toLists;
    private Handler mHandler = new Handler();
    private ArriveShopHomeView arriveShopHomeView;

    public void setArriveShopHomeView(ArriveShopHomeView arriveShopHomeView) {
        this.arriveShopHomeView = arriveShopHomeView;
    }

    public static ClothingArriveShopFragment newInstance() {
        ClothingArriveShopFragment childFragment = new ClothingArriveShopFragment();
        return childFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_clothing_arrive_shop, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        setListener();
        initAdapter();
        updateCount();
        return view;
    }


    @Override
    protected ArriveShopPesenter createPresenter() {
        return new ArriveShopPesenter(this);
    }

    @Override
    protected void initView() {
        codeDescribe.setText("出厂衣物条码：");
        arriveToFactory.setVisibility(View.INVISIBLE);
        leaveFactoryText.setText("出厂衣物");
        toLists = new ArrayList<>();
        searchClothcode.requestFocus();
    }

    private void updateCount() {
        arriveShopClothingFrom.setText(toLists.size() + "");
        arriveShopClothingTo.setText(toLists.size() + "");
    }

    private void setListener() {
        searchClothcode.setOnEditorActionListener(mOnEditorActionListener);
    }

    private TextView.OnEditorActionListener mOnEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            L.i("event=" + event + "actionId=" + actionId);
            if (!CheckUtils.isNull(event) && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    || actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
                scanQRcode(v);
            }

            return false;
        }
    };


    private void scanQRcode(TextView v) {
        search2Click();
        hideSoftInput();
    }


    private void initAdapter() {
        mToAdapter = new ReceiveProClothingListAdapter(activity, toLists);
        mToAdapter.setShowBackgroundColor(true);
        mToRecycler.setLayoutManager(new LinearLayoutManager(activity));
        mToRecycler.addItemDecoration(new DividerDecoration(getActivity(), DividerDecoration.VERTICAL_LIST));
        mToRecycler.setAdapter(mToAdapter);
        mToRecycler.setFocusable(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mHandler.removeCallbacksAndMessages(null);
        unbinder.unbind();
    }


    @OnClick({R.id.fragment_arrive_shop_search1_btn, R.id.remark_btn, R.id.arrive_show_b1, R.id.arrive_show_b2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.remark_btn:
//                if (CheckUtils.isNull(mReceiveStoreBean)) return;
//                CrippledDialogFragment mRemarkDialog = new CrippledDialogFragment();
//                Bundle mBundle = new Bundle();
//                mBundle.putBoolean(ARG_PARAM1, true);
//                mBundle.putString(ARG_PARAM2, mReceiveStoreBean.getRemark());
//                mRemarkDialog.setArguments(mBundle);
//                mRemarkDialog.setOnSureClickListener(mDialogListener);
//                mRemarkDialog.show(getChildFragmentManager(), CrippledDialogFragment.class.getName());
                break;
            case R.id.fragment_arrive_shop_search1_btn:
                scanQRcode(fragmentArriveShopSearch1Btn);
                break;
            case R.id.arrive_show_b1:
                arriveShopHomeView.showPositionFragment(0, 2);
                SaveParamets.saveArriveShopPosition(0);
                break;
            case R.id.arrive_show_b2:
                arriveShopHomeView.showPositionFragment(1, 2);
                SaveParamets.saveArriveShopPosition(1);
                break;
        }
    }

    private OnDialogSureClickListener mDialogListener = new OnDialogSureClickListener() {
        @Override
        public void onSureItemClick(Object message) {
//            String mstrRemark = (String) message;
//            mvpPresenter.addRemark(_mActivity, orderId, mstrRemark);
        }
    };

    private void search2Click() {
        String strClothcode = getText(searchClothcode);
        if (CheckUtils.isEmpty(strClothcode)) return;
        hideSoftInput();
        searchClothcode.setText("");
        //衣物到店
        mvpPresenter.clothingStore(_mActivity, strClothcode);
    }


    /**
     * 搜索左边
     *
     * @param model
     */
    @Override
    public void loadReceiveSucceed(ReceiveStoreBean model) {
    }

    @Override
    public void loadReceiveProListSucceed(List<ReceiveProListBean> model) {
    }

    @Override
    public void loadClothingArriveSuccessd(ReceiveProClothingBean model) {
        searchClothcode.requestFocus();
        if (null != model && null != model.getProList() && model.getProList().size() > 0) {
            if (toLists.size() > 0) {
                //去重复
                List<ReceiveClothingBean> liShiBeans = new ArrayList<>();
                for (ReceiveClothingBean bean : model.getProList()) {
                    if (toLists.contains(bean)) {
                        liShiBeans.add(bean);
                    }
                }
                //移除
                for (ReceiveClothingBean bean : liShiBeans) {
                    model.getProList().remove(bean);
                }
            }
            toLists.addAll(0, model.getProList());
            mToAdapter.setDatas(toLists);
            code.setText(model.getScanCode());
            factoryWorkTime.setText(MyDateUtils.formatDataTime(model.getCreateTime()));
            arriveShopOperator.setText(SaveParamets.getSaveUseName(_mActivity));/*操作人*/
            //更新上面
            SearchrequestFocus();
            updateCount();
        }
    }

    private void SearchrequestFocus() {
        searchClothcode.setText("");
        SystemUtils.hideInputmethod(searchClothcode);
    }

    /**
     * 衣物确认到店
     *
     * @param message
     * @param from
     */
    @Override
    public void loadConfirmStoreSucceed(String message, ReceiveProListBean from) {
    }

    @Override
    public void loadAddRemark(String msg, String addRemark) {
        T.show(msg);
//        mReceiveStoreBean.setRemark(addRemark);
        remark.setText(getString(R.string.management_note) + addRemark);
    }

    @Override
    public void loadFail(String message) {
        T.show(message);
        searchClothcode.setText("");
        searchClothcode.requestFocus();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        searchClothcode.requestFocus();
    }

    @Override
    public void loadAallStore(String message) {
    }

    @Override
    public void allStoreFinish() {
    }
}
